
package guoan.web.weixin;

import guoan.domain.AutoForm;
import guoan.domain.AutoFormInput;
import guoan.domain.AutoFormValue;
import guoan.service.AutoFormService;
import guoan.utils.CommonUtils;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/weixin/autoForm")
public class WeiXinAutoFormController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private AutoFormService autoFormService;

    /** 保存预约考试 */
    @RequestMapping(value = "show", method = RequestMethod.GET)
    public String show(Long autoFormId, Model model){
        Preconditions.checkNotNull(autoFormId, "参数错误");
        List<AutoFormInput> autoFormInputs = autoFormService.findByAutoFormIdOrderByOrderIdDesc(autoFormId);
        AutoForm autoForm = autoFormService.findOne(autoFormId);
        model.addAttribute("autoForm", autoForm);
        model.addAttribute("autoFormInputs", autoFormInputs);

        return "/weixin/autoForm/show";
    }

    /** 保存预约考试 */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, Long autoFormId, Model model){
        Preconditions.checkNotNull(autoFormId, "参数错误");
        Enumeration<String> pNames = request.getParameterNames();
        String formId = CommonUtils.randomPassword();
        List<AutoFormValue> autoFormValueList = Lists.newArrayList();
        while (pNames.hasMoreElements()) {
            String name = pNames.nextElement();
            if (name.indexOf("formInput_") > -1) {
                String[] paramInput = name.split("formInput_");
                if (paramInput.length > 0) {
                    AutoFormValue autoFormValue = new AutoFormValue();
                    Long inputId = Long.valueOf(paramInput[1]);
                    String value = request.getParameter(name);
                    autoFormValue.setAutoFormId(autoFormId);
                    autoFormValue.setAutoFormInputId(inputId);
                    autoFormValue.setFormId(formId);
                    autoFormValue.setValue(value);
                    autoFormValue.setCreateTime(new DateTime());
                    autoFormValueList.add(autoFormValue);
                }
            }

        }
        autoFormService.addAutoFormValue(autoFormValueList);
        return "redirect:/weixin/autoForm/success";
    }

    @RequestMapping(value = "success", method = RequestMethod.GET)
    public String success(){
        return "/weixin/autoForm/msg";
    }

}
